package com.powergisol.gis.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.powergisol.gis.vo.B3DMJsonData;
import com.powergisol.gis.vo.CRSInfo;
import com.powergisol.gis.vo.FeatureType;
import com.powergisol.gis.vo.LOBJsonData;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DataChangeUtil {
    public static void main(String[] args) {
        FeatureType data = b3dmToEntity("E:\\tileset.json");
        System.out.println(data);
    }
    /**
     * lob json文件读取转换
     */
    public static FeatureType lobToEntity(String lob){
        FeatureType featureType = new FeatureType();
        try {
            LOBJsonData lobData = FileRead.fileReadLob(lob);
            double minx = 0;
            double maxx = 0;
            double miny = 0;
            double maxy = 0;
            if(null != lobData.getDataDefine()){
                LOBJsonData.DataDefineBean.RangeBean rangeBean = lobData.getDataDefine().getRange();
                minx = Double.valueOf(rangeBean.getWest());
                maxx = Double.valueOf(rangeBean.getEast());
                miny = Double.valueOf(rangeBean.getSouth());
                maxy = Double.valueOf(rangeBean.getNorth());
            }else if(null != lobData.getRoot()){
                List<Double> box = lobData.getRoot().getBoundingVolume().getBox();
                minx = box.get(0);
                maxx = box.get(1);
                miny = box.get(2);
                maxy = box.get(3);
            }
            FeatureType.NativeBoundingBox nativeBoundingBox =
                    new FeatureType.NativeBoundingBox(minx,maxx,miny,maxy,null);
            featureType.setNativeBoundingBox(nativeBoundingBox);
        }catch (IOException e) {
            e.printStackTrace();
        }
        return featureType;
    }

    /**
     * b3dm json文件读取转换
     * @param b3dm
     * @return
     */
    public static FeatureType osgbTb3dmToEntity(String b3dm){
        FeatureType featureType = new FeatureType();
        try {
            B3DMJsonData b3dmData = FileRead.fileReadB3dm(b3dm);
            double minx = 0;
            double maxx = 0;
            double miny = 0;
            double maxy = 0;
            List<Double> box = b3dmData.getRoot().getBoundingVolume().getBox();
            if(null == box){
                box = b3dmData.getRoot().getBoundingVolume().getSphere();
            }
            if(null != box){
                minx = box.get(0);
                maxx = box.get(1);
                miny = box.get(2);
                maxy = box.get(3);
            }
            FeatureType.NativeBoundingBox nativeBoundingBox =
                    new FeatureType.NativeBoundingBox(minx,maxx,miny,maxy,null);
            featureType.setNativeBoundingBox(nativeBoundingBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return featureType;
    }
    /**
     * b3dm json文件读取转换
     * @param b3dm
     * @return
     */
    public static FeatureType b3dmToEntity(String b3dm){
        FeatureType featureType = new FeatureType();
        try {
            B3DMJsonData b3dmData = FileRead.fileReadB3dm(b3dm);
            double minx = 0;
            double maxx = 0;
            double miny = 0;
            double maxy = 0;

            List<Double> sphere = b3dmData.getRoot().getBoundingVolume().getSphere();
            if(null == sphere){
                sphere = b3dmData.getRoot().getBoundingVolume().getBox();
            }
            if(null != sphere){
                minx = sphere.get(0);
                maxx = sphere.get(1);
                miny = sphere.get(2);
                maxy = sphere.get(3);
            }
            FeatureType.NativeBoundingBox nativeBoundingBox =
                    new FeatureType.NativeBoundingBox(minx,maxx,miny,maxy,null);
            featureType.setNativeBoundingBox(nativeBoundingBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return featureType;
    }
    /**
     * 调用接口json转换
     * @param object
     * @return
     */
    public static FeatureType jsonToEntity(JSONObject object){
        FeatureType featureType = new FeatureType();
        JSONObject crss = JSONObject.parseObject(object.getString("nativeCRS"));
        if(null != crss){
            CRSInfo info = new CRSInfo();
            info.setWkid(crss.getString("wkid"));
            info.setName(crss.getString("name"));
            info.setDatum(crss.getString("datum"));
            info.setWkt(crss.getString("wkt"));
            featureType.setNativeCRS(info);
        }
        JSONObject attributes = JSONObject.parseObject(object.getString("attributes"));
        if(null != attributes){
            JSONArray array = attributes.getJSONArray("attribute");
            FeatureType.Attributes attributes1 = new FeatureType.Attributes();
            List<FeatureType.Attributes.Attribute> list = new ArrayList<>();
            for(int i=0;i<array.size();i++ ){
                JSONObject attribute = array.getJSONObject(i);
                FeatureType.Attributes.Attribute attr = new FeatureType.Attributes.Attribute();
                attr.setName(attribute.getString("name"));
                attr.setMaxOccurs(attribute.getInteger("maxOccurs"));
                attr.setMinOccurs(attribute.getInteger("minOccurs"));
                attr.setType(attribute.getString("type"));
                attr.setNillable(attribute.getBoolean("nillable"));
                list.add(attr);
            }
            attributes1.setAttribute(list);
            featureType.setAttributes(attributes1);
        }

        JSONObject jsonNative = JSONObject.parseObject(object.getString("nativeBoundingBox"));
        if(null != jsonNative){
            double minx = jsonNative.getDouble("minx");
            double maxx = jsonNative.getDouble("maxx");
            double miny = jsonNative.getDouble("miny");
            double maxy = jsonNative.getDouble("maxy");
            String crs1 = jsonNative.getString("crs");
            FeatureType.NativeBoundingBox nativeBoundingBox =
                    new FeatureType.NativeBoundingBox(minx,maxx,miny,maxy,crs1);
            featureType.setNativeBoundingBox(nativeBoundingBox);
        }

        return featureType;
    }
}
